package com.ktjy.ch15.service.impl;

import com.ktjy.ch15.entity.Crop;
import com.ktjy.ch15.mapper.CropMapper;
import com.ktjy.ch15.service.CropService;
import com.ktjy.ch15.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("cropService")
public class CropServiceImpl implements CropService {

    @Autowired
    private CropMapper cropMapper;

    @Override
    public Page<Crop> findPage(Crop crop, Integer pageNo, Integer pageSize) {
        Page<Crop> page = new Page<Crop>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        // 查询总数，计算总页数
        Integer count = cropMapper.count(crop);
        page.setTotalCount(count);
        // 查询分页数据
        Integer pageBegin=(pageNo-1)*pageSize;
        List<Crop> data = cropMapper.getPage(crop, pageBegin, pageSize);
        page.setData(data);
        return page;
    }

    @Override
    public boolean save(Crop crop) {
        return cropMapper.insert(crop) > 0;
    }

    @Override
    public Crop findById(Long cropId) {
        return cropMapper.getById(cropId);
    }

    @Override
    public boolean update(Crop crop) {
        return cropMapper.update(crop) > 0;
    }

    @Override
    public boolean delete(Long id) {
        return cropMapper.delete(id) > 0;
    }

    @Override
    public Crop findByCropName(String cropName) {
        return cropMapper.getByCropName(cropName);
    }
}
